/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.magri.mvc.pesquisa.persistence;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import org.magri.mvc.persistence.AbstractDAO;
import org.magri.mvc.persistence.DBUtil;
import org.magri.mvc.persistence.DefaultDAO;
import org.magri.mvc.pesquisa.modelo.Tabela;

/**
 *
 * @author Magri
 */
public class PesquisaDAO extends DefaultDAO {
    
    
    public Tabela executarConsulta(String sql, String parametro) {
        Tabela tabela = new Tabela();
        try {
            PreparedStatement ps = dbConnection.getConnection().prepareStatement(sql);
            ps.setString(1, parametro);
            ResultSet rs = ps.executeQuery();
            
            
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                tabela.adicionaColuna(metaData.getColumnLabel(i));
            }
            
            while (rs.next()) {
                Vector<Object> novaLinha = new Vector<Object>();
                for (int i = 1; i <= columnCount; i++) {
                    novaLinha.add(rs.getObject(i));
                }
                tabela.adicionaLinha(novaLinha);
            }
            
            DBUtil.close(rs, ps);
        } catch (Exception e) {
        }
        return tabela;
    }
    
}
